package com.Lemon.Zhkuhealth.admin.controller;

import com.Lemon.Zhkuhealth.common.constant.MessageConstant;
import com.Lemon.Zhkuhealth.common.pojo.entity.CheckItem;
import com.Lemon.Zhkuhealth.common.util.PageResult;
import com.Lemon.Zhkuhealth.common.util.QueryPageBean;
import com.Lemon.Zhkuhealth.common.util.R;
import com.Lemon.Zhkuhealh.api.service.ICheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Some description here.
 *
 * @author LanPingX.com
 * @version 1.0, 2023/08/21
 */
@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @DubboReference
    ICheckItemService checkItemService;

    /**
     * 查询所有记录，检查组需要用到
     * @return
     */
    @GetMapping("/findAll")
    public R findAll() {
        List<CheckItem> checkItemList = checkItemService.list();
        if (checkItemList != null && !checkItemList.isEmpty()) {
            return new R(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        }
        return new R(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }



    @PostMapping("/add")
    public R add(@RequestBody CheckItem checkItem) {
        // 实现添加功能
        try {
            checkItemService.save(checkItem);
        } catch (Exception e) {
            log.error("添加检查项失败, {}", e.getMessage());
            return new R(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new R(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @PostMapping("/findPage")
    public PageResult<CheckItem> findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult<CheckItem> checkItemPageResult = checkItemService.page(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return checkItemPageResult;
    }

    @GetMapping("/findById")
    public R findById(Long id) {
        CheckItem checkItem = checkItemService.getById(id);
        return new R(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    @PostMapping("/update")
    public R update(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.updateById(checkItem);
        } catch (Exception e) {
            log.error("更新检查项失败, {}", e.getMessage());
            return new R(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new R(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @DeleteMapping("/delete")
    public R delete(Long id) {
        try {
            checkItemService.deleteById(id);
        } catch (Exception e) {
            log.error("删除检查项失败, {}", e.getMessage());
            return new R(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new R(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }


}
